<template>
  <div>
    <el-dialog
      v-model="dialogVisible"
      width="80%"
      align-center
      :show-close="false"
      @open="handleOpen"
      @close="handleClose"
      destroy-on-close
      draggable
    >
      <template #header="{ close }">
        <div class="dialog-header flex justify-between items-center">
          <div>{{ title }}</div>
          <el-icon class="el-icon--left cursor-pointer" @click="close"
            ><CircleCloseFilled color="#fff"
          /></el-icon>
        </div>
      </template>
      <el-form :model="editForm" :rules="rules" label-width="135px" ref="editFormRef">
        <!-- 原项目信息 -->
        <div class="bg-white my-[10px]">
          <BaseHead>原项目信息</BaseHead>
          <div class="p-[10px]">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="原项目名称" prop="sourceProjectName">
                  <el-autocomplete
                    v-model="editForm.sourceProjectName"
                    :fetch-suggestions="queryOriginalProject"
                    clearable
                    placeholder="请输入检索关键词"
                    value-key="name"
                    highlight-first-item
                    @select="selectOriginalProject"
                    @blur="selectOriginalBlur"
                  >
                    <template #default="{ item }">
                      <span>{{ item.sourceProjectName }}</span>
                      <!-- <span class="mx-[10px]">{{ item.account }}</span>
                      <span>{{ item.unitName }}</span> -->
                    </template>
                  </el-autocomplete>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="原项目分类" prop="sourceProjectTypeValue"
                  >{{ editForm.sourceProjectTypeValue }}
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="原项目负责人" prop="sourceChargerName">
                  <el-input
                    v-model="editForm.sourceChargerName"
                    placeholder="原项目负责人"
                    clearable
                /></el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="原项目合同金额" prop="sourceFeeAuthorize">
                  {{ editForm.sourceFeeAuthorize }}万元</el-form-item
                >
              </el-col>
              <el-col :span="12">
                <el-form-item label="原项目签订日期" prop="sourceAuthorizeDate">
                  {{ editForm.sourceAuthorizeDate }}</el-form-item
                >
              </el-col>
              <el-col :span="12">
                <el-form-item label="原项目签结束期" prop="sourceEndDate"
                  >{{ editForm.sourceEndDate }}
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </div>
        <!-- 基本信息 -->
        <div class="bg-white mb-[10px]">
          <BaseHead>基本信息</BaseHead>
          <div class="p-[10px]">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="合同名称" prop="name">
                  <el-input v-model="editForm.name" placeholder="合同名称" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="合同负责人" prop="chargerName">
                  <el-autocomplete
                    v-model="editForm.chargerName"
                    :fetch-suggestions="queryPerson"
                    clearable
                    placeholder="请输入检索关键词"
                    value-key="name"
                    highlight-first-item
                    @select="selectPerson"
                    @blur="chargerPerosnBlur"
                    :disabled="verifyRoleType.isKyry()"
                  >
                    <template #default="{ item }">
                      <span>{{ item.name }}</span>
                      <span class="mx-[10px]">{{ item.account }}</span>
                      <span>{{ item.unitName }}</span>
                    </template>
                  </el-autocomplete>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="负责人电话" prop="chargerTel">
                  <el-input v-model="editForm.chargerTel" placeholder="负责人电话" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="合同金额" prop="feeAuthorize">
                  <el-input
                    v-model="editForm.feeAuthorize"
                    placeholder="合同金额"
                    clearable
                    @input="fomateNumber(editForm, 'feeAuthorize', $event)"
                  >
                    <template #append>万元</template>
                  </el-input>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="所属单位" prop="unitId">
                  <CommonTreeSelect
                    v-model="editForm.unitId"
                    valueKey="codeColumn"
                    labelKey="valueColumn"
                    paramsName="成果归属单位"
                    disabled
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="交付形式" prop="deliveryForm">
                  <CommonSelect
                    paramsName="交付形式"
                    placeholder="交付形式"
                    v-model="editForm.deliveryForm"
                    valueKey="code"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="签订日期" prop="authorizeDate">
                  <el-date-picker
                    v-model="editForm.authorizeDate"
                    type="date"
                    clearable
                    placeholder="签订日期"
                    value-format="YYYY-MM-DD"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="开始日期" prop="beginDate">
                  <el-date-picker
                    v-model="editForm.beginDate"
                    type="date"
                    clearable
                    placeholder="开始日期"
                    value-format="YYYY-MM-DD"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="结束日期" prop="planEndDate">
                  <el-date-picker
                    v-model="editForm.planEndDate"
                    type="date"
                    clearable
                    placeholder="结束日期"
                    value-format="YYYY-MM-DD"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="合同状态" prop="projectStatusId">
                  <CommonRadio
                    paramsName="项目状态"
                    v-model="editForm.projectStatusId"
                    valueKey="code"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="甲方是否盖章" prop="buyerIsSeal">
                  <el-radio-group v-model="editForm.buyerIsSeal">
                    <el-radio value="是">是</el-radio>
                    <el-radio value="否">否</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方是否盖章" prop="yfIsSeal">
                  <el-radio-group v-model="editForm.yfIsSeal">
                    <el-radio value="是">是</el-radio>
                    <el-radio value="否">否</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="合同是否回收" prop="isRecover">
                  <el-radio-group v-model="editForm.isRecover">
                    <el-radio value="是">是</el-radio>
                    <el-radio value="否">否</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="合同电子版" prop="fileIds">
                  <FileUpload :uploadUrl="uploadUrl" v-model="htFileList" @change="htFileChange" />
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="盖章页电子版" prop="signature">
                  <FileUpload :uploadUrl="uploadUrl" v-model="gzFileList" @change="gzFileChange" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </div>
        <!-- 用章信息不要了 -->
        <!-- 供方信息  -->
        <div class="bg-white">
          <BaseHead>供方信息</BaseHead>
          <div class="p-[10px]">
            <el-row :gutter="20">
              <el-col :span="12">
                <el-form-item label="供方单位" prop="yfUnitName">
                  <el-autocomplete
                    v-model="editForm.yfUnitName"
                    :fetch-suggestions="queryGfUnit"
                    clearable
                    placeholder="请输入检索关键词"
                    @select="selectGfUnit"
                  >
                    <template #default="{ item }">
                      <span>{{ item.name }}</span>
                      <span class="mx-[10px]">{{ item.account }}</span>
                      <span>{{ item.unitName }}</span>
                    </template>
                  </el-autocomplete>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方单位性质" prop="yfUnitKind">
                  <CommonSelect
                    paramsName="甲方类别"
                    placeholder="供方单位性质"
                    v-model="editForm.yfUnitKind"
                    valueKey="code"
                  />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方社会信用代码" prop="yfCode">
                  <el-input v-model="editForm.yfCode" placeholder="供方社会信用代码" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方联系人名称" prop="yfChargerName">
                  <el-input v-model="editForm.yfChargerName" placeholder="供方联系名称" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方电话" prop="yfTel">
                  <el-input v-model="editForm.yfTel" placeholder="供方电话" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方所属省份" prop="yfArea">
                  <el-select
                    v-model="editForm.yfArea"
                    placeholder="供方所属省份"
                    clearable
                    @change="provinceChange"
                  >
                    <el-option
                      v-for="item in provinceList.value"
                      :key="item.codeColumn"
                      :label="item.valueColumn"
                      :value="item.codeColumn"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方所属地市" prop="yfCity">
                  <el-select v-model="editForm.yfCity" placeholder="单位所属地市" clearable>
                    <el-option
                      v-for="item in cityList.value"
                      :key="item.id"
                      :label="item.label"
                      :value="item.id"
                    />
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方收款人" prop="yfPayee">
                  <el-input v-model="editForm.yfPayee" placeholder="供方收款人" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方开户银行" prop="yfBankName">
                  <el-input v-model="editForm.yfBankName" placeholder="供方开户银行" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方开户账号" prop="yfBankAccount">
                  <el-input v-model="editForm.yfBankAccount" placeholder="供方开户账号" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方国别" prop="yfNationality">
                  <el-input v-model="editForm.yfNationality" placeholder="供方国别" clearable />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="供方是否海外单位" prop="yfIsOverseas">
                  <el-radio-group v-model="editForm.yfIsOverseas">
                    <el-radio value="是">是</el-radio>
                    <el-radio value="否">否</el-radio>
                  </el-radio-group>
                </el-form-item>
              </el-col>
              <el-col :span="24">
                <el-form-item label="备注" prop="note">
                  <el-input v-model="editForm.note" placeholder="备注" type="textarea" />
                </el-form-item>
              </el-col>
            </el-row>
          </div>
        </div>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <div class="operate-btns">
            <!-- checkStatus == '' 表示新增 -->
            <!-- <template v-if="verifyRoleType.isKyry()">
              <el-button type="primary" @click="save(-1)" :loading="loading">暂存</el-button>
              <el-button type="primary" @click="save(1)" :loading="loading">提交</el-button>
            </template>
            <template v-if="verifyRoleType.isKyms()">
              <el-button type="primary" @click="save(-1)" :loading="loading">保存</el-button>
              <el-button
                type="primary"
                @click="save(2)"
                :loading="loading"
                v-if="editForm.isAuditor"
                >保存并通过</el-button
              >
            </template>
            <template v-if="verifyRoleType.iskycgly()">
              <el-button type="primary" @click="save(-1)" :loading="loading">保存</el-button>
              <el-button
                type="primary"
                @click="save(2)"
                v-if="
                  checkStatus == '' || [-1, 3].includes(Number(checkStatus)) || editForm.isAuditor
                "
                :loading="loading"
                >保存并通过</el-button
              >
            </template> -->
            <el-button
              type="primary"
              @click="save(-1)"
              :loading="loading && submitType == -1"
              :disabled="loading && submitType != -1"
              >{{ verifyRoleType.isKyry() ? "暂存" : "保存" }}</el-button
            >
            <el-button
              type="primary"
              @click="save(1)"
              :loading="loading && submitType == 1"
              :disabled="loading && submitType != 1"
              v-if="verifyRoleType.isKyry()"
              >提交</el-button
            >
            <el-button
              type="primary"
              @click="save(2)"
              :loading="loading && submitType == 2"
              :disabled="loading && submitType != 2"
              v-if="
                (verifyRoleType.isKyms() && editForm.isAuditor) ||
                (verifyRoleType.iskycgly() &&
                  (checkStatus == '' ||
                    [-1, 3].includes(Number(checkStatus)) ||
                    editForm.isAuditor))
              "
              >保存并通过</el-button
            >
            <el-button type="primary" @click="dialogVisible = false">关闭</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import BaseHead from "@/views/project/components/BaseHead.vue";
import CommonSelect from "@/views/project/components/CommonSelect.vue";
import CommonRadio from "@/views/project/components/CommonRadio.vue";
import CommonTreeSelect from "@/views/project/components/CommonTreeSelect.vue";
import FileUpload from "@/views/project/components/FileUpload.vue";
import { getPersonList } from "@/api/project/common";
import { getList } from "@/api/project/yf/htgfdw";
import { addRecords, editRecords, findOriginalProject, detail } from "@/api/project/yf/czhtqd";
import { ElMessage } from "element-plus";
import { assignObj, verifyRoleType, fomateNumber } from "@/utils/main";
import useUserStore from "@/store/modules/user";
import useCommonFetch from "@/hooks/useCommonFetch";
const userStore = useUserStore();
const dialogVisible = ref(false);
const editFormRef = ref();
const uploadUrl = `/commonFile/uploadFile?modelName=出账合同签订`;
const htFileList = ref([]); //合同附件列表
const gzFileList = ref([]); //盖章附件列表
const checkStatus = ref(""); //审核状态 用于判断按钮显示用
const provinceList = ref([]);
const cityList = ref([]);
const operateId = ref(null);
const detailData = ref(null);
const title = computed(() => {
  return operateId.value ? "出账合同编辑" : "出账合同新增";
});
const loading = ref(false);
const submitType = ref(null);
const editForm = reactive({
  //原项目信息
  sourceProjectId: "", //原项目id
  sourceProjectName: "", //原项目名称
  sourceProjectType: "", //原项目分类code
  sourceProjectTypeValue: "", //原项目分类名称
  sourceProjectClass: "", //原项目性质
  sourceChargerName: "", //原项目负责人
  sourceFeeAuthorize: "", //原项目合同金额
  sourceAuthorizeDate: "", // 原项目签订日期
  sourceEndDate: "", //原项目结束日期
  sourceSubjectId: "", //原项目一级学科
  sourceSubjectClassId: "", //原项目教育部统计归属
  //合同信息
  name: "", //合同名称
  chargerName: "", //合同负责人
  chargerCode: "", //负责人id
  chargerTel: "", //负责人电话
  feeAuthorize: "", //合同金额
  unitId: "", //所属单位
  deliveryForm: "", //交付形式
  authorizeDate: "", //签订日期
  beginDate: "", //开始日期
  planEndDate: "", //结束日期
  projectStatusId: "", //合同状态
  buyerIsSeal: "", //甲方是否盖章
  yfIsSeal: "", //供方是否盖章
  isRecover: "", //合同是否回收
  fileIds: "", //合同电子版
  signature: "", //盖章页电子版
  //供方信息
  yfUnitName: "", //供方单位
  yfUnitKind: "", //供方单位性质
  yfCode: "", //供方社会信用代码
  yfChargerName: "", //供方联系人名称
  yfTel: "", //供方电话
  yfArea: "", //供方所属省份
  yfCity: "", //供方所属地市
  yfPayee: "", //供方收款人
  yfBankName: "", //供方开户银行
  yfBankAccount: "", //供方开户账号
  yfNationality: "", //供方国别
  yfIsOverseas: "", //供方是否海外单位
  note: "", //备注
  taskId: "", //任务id
  isAuditor: "", //当前是否能审核
});
//验证签订日期
const validateAuthorizeDate = (rule, value, callback) => {
  if (editForm.beginDate) {
    if (new Date(value).getTime() >= new Date(editForm.beginDate).getTime()) {
      callback(new Error("签订日期必须早于开始日期"));
    } else {
      callback();
    }
  } else {
    callback();
  }
};
//验证开始日期
const validateBeginDate = (rule, value, callback) => {
  if (editForm.authorizeDate || editForm.planEndDate) {
    if (editForm.authorizeDate && editForm.planEndDate) {
      if (
        new Date(value).getTime() <= new Date(editForm.authorizeDate).getTime() &&
        new Date(value).getTime() >= new Date(editForm.planEndDate).getTime()
      ) {
        callback(new Error("开始日期必须早于结束日期和晚于签订日期"));
      } else if (new Date(value).getTime() <= new Date(editForm.authorizeDate).getTime()) {
        callback(new Error("开始日期必须晚于签订日期"));
      } else if (new Date(value).getTime() >= new Date(editForm.planEndDate).getTime()) {
        callback(new Error("开始日期必须早于结束日期"));
      } else {
        callback();
      }
    } else if (editForm.authorizeDate) {
      if (new Date(value).getTime() <= new Date(editForm.authorizeDate).getTime()) {
        callback(new Error("开始日期必须晚于签订日期"));
      } else {
        callback();
      }
    } else if (editForm.planEndDate) {
      if (new Date(value).getTime() >= new Date(editForm.planEndDate).getTime()) {
        callback(new Error("开始日期必须早于结束日期"));
      } else {
        callback();
      }
    }
  } else {
    callback();
  }
};
//验证结束日期
const validatePlanEndDate = (rule, value, callback) => {
  if (editForm.beginDate) {
    if (new Date(value).getTime() <= new Date(editForm.beginDate).getTime()) {
      callback(new Error("结束日期必须晚于开始日期"));
    } else {
      callback();
    }
  } else {
    callback();
  }
};
const rules = {
  sourceProjectName: [
    {
      required: true,
      message: "原项目名称必填",
    },
  ],
  name: [
    {
      required: true,
      message: "合同名称必填",
    },
  ],
  chargerName: [
    {
      required: true,
      message: "负责人必填",
    },
  ],
  chargerTel: [
    {
      required: true,
      message: "负责人电话必填",
    },
  ],
  feeAuthorize: [
    {
      required: true,
      message: "合同金额必填",
    },
  ],
  deliveryForm: [
    {
      required: true,
      message: "交付形式必填",
    },
  ],
  authorizeDate: [
    {
      required: true,
      message: "签订日期必填",
    },
    { validator: validateAuthorizeDate, trigger: "blur" },
  ],
  beginDate: [
    {
      required: true,
      message: "开始日期必填",
    },
    { validator: validateBeginDate, trigger: "blur" },
  ],
  planEndDate: [
    {
      required: true,
      message: "结束日期必填",
    },
    { validator: validatePlanEndDate, trigger: "blur" },
  ],
  projectStatusId: [
    {
      required: true,
      message: "合同状态必填",
    },
  ],
  yfIsSeal: [
    {
      required: true,
      message: "供方是否盖章必填",
    },
  ],
  fileIds: [
    {
      required: true,
      message: "请上传合同电子版",
    },
  ],
  yfUnitName: [
    {
      required: true,
      message: "供方单位必填",
    },
  ],
  yfUnitKind: [
    {
      required: true,
      message: "供方单位性质必填",
    },
  ],
  yfCode: [
    {
      required: true,
      message: "供方社会信用代码必填",
    },
  ],
  yfChargerName: [
    {
      required: true,
      message: "供方联系人名称必填",
    },
  ],
  yfTel: [
    {
      required: true,
      message: "供方电话必填",
    },
  ],
};
//获取记录详情
const getDetail = async () => {
  try {
    const res = await detail(operateId.value);
    detailData.value = res.data;
    checkStatus.value = res.data.checkstatus;
    htFileList.value = res.data.fileList ? res.data.fileList : [];
    gzFileList.value = res.data.signatureList ? res.data.signatureList : [];
  } catch (error) {
    console.log(error);
  }
};
//选择的省份改变
const provinceChange = () => {
  editForm.yfCity = "";
  cityList.value = useCommonFetch("甲方所属地市", editForm.yfArea);
};
// 新增
const add = () => {
  operateId.value = null;
  dialogVisible.value = true;
  //科研人员新增默认负责人是自己
  if (verifyRoleType.isKyry()) {
    editForm.chargerName = userStore.nickName;
    editForm.chargerCode = userStore.userRealId;
    editForm.chargerTel = userStore.phonenumber;
    editForm.unitId = userStore.deptId;
  }
};
// 编辑
const edit = async (id) => {
  operateId.value = id;
  dialogVisible.value = true;
  await getDetail();
  nextTick(async () => {
    //表单表格赋值
    assignObj(editForm, detailData.value);
    cityList.value = useCommonFetch("甲方所属地市", editForm.yfArea);
  });
};
defineExpose({ add, edit });
//根据名称查询项目
const queryOriginalProject = async (queryString, cb) => {
  if (queryString.trim() == "") {
    return;
  }
  editForm.sourceProjectId = "";
  const params = {
    originalProjectName: queryString,
  };
  try {
    const res = await findOriginalProject(params);
    cb(res.data);
  } catch (error) {
    console.log(error);
  }
};
//选择的原项目改变
const selectOriginalProject = (val) => {
  editForm.sourceProjectId = val.sourceProjectId;
  editForm.sourceProjectName = val.sourceProjectName;
  editForm.sourceProjectType = val.sourceProjectType;
  editForm.sourceProjectTypeValue = val.sourceProjectTypeValue;
  editForm.sourceProjectClass = val.sourceProjectClass;
  editForm.sourceChargerName = val.sourceChargerName;
  editForm.sourceFeeAuthorize = val.sourceFeeAuthorize;
  editForm.sourceAuthorizeDate = val.sourceAuthorizeDate;
  editForm.sourceEndDate = val.sourceEndDate;
  editForm.sourceSubjectId = val.sourceSubjectId;
  editForm.sourceSubjectClassId = val.sourceSubjectClassId;
};
//选择项目输入框失去焦点
const selectOriginalBlur = () => {
  if (!editForm.sourceProjectId) {
    editForm.sourceProjectId = "";
    editForm.sourceProjectName = "";
    editForm.sourceProjectType = "";
    editForm.sourceProjectTypeValue = "";
    editForm.sourceProjectClass = "";
    editForm.sourceChargerName = "";
    editForm.sourceFeeAuthorize = "";
    editForm.sourceAuthorizeDate = "";
    editForm.sourceEndDate = "";
    editForm.sourceSubjectId = "";
    editForm.sourceSubjectClassId = "";
  }
};
//根据姓名查询合同负责人
const queryPerson = async (queryString, cb) => {
  if (queryString.trim() == "") {
    return;
  }
  editForm.chargerCode = "";
  const params = {
    name: queryString,
  };
  try {
    const res = await getPersonList(params);
    cb(res.data);
  } catch (error) {
    console.log(error);
  }
};
//选择合同负责人改变
const selectPerson = (val) => {
  editForm.chargerCode = val.id;
  editForm.chargerTel = val.mobile;
  editForm.unitId = val.unitId;
};
//清空负责人字段的值
const resetChargerPerosn = () => {
  editForm.chargerName = "";
  editForm.chargerCode = "";
  editForm.chargerTel = "";
  editForm.unitId = "";
};
//选择负责人框失去焦点
const chargerPerosnBlur = () => {
  if (!editForm.chargerCode) {
    resetChargerPerosn();
  }
};
//根据名称查合同供方单位
const queryGfUnit = async (queryString, cb) => {
  if (queryString.trim() == "") {
    return;
  }
  const params = {
    name: editForm.yfUnitName,
    validType: "0",
    pageNum: 1,
    pageSize: 1000,
  };
  try {
    const res = await getList(params);
    cb(res.rows);
  } catch (error) {
    console.log(error);
  }
};
//选择的合同供方单位改变
const selectGfUnit = (val) => {
  editForm.yfUnitName = val.name;
  editForm.yfUnitKind = val.buyerType;
  editForm.yfCode = val.code;
  editForm.yfChargerName = val.contacts; //供方联系人名称
  editForm.yfTel = val.telephone; //供方电话
  editForm.yfArea = val.province; //供方所属省份
  editForm.yfCity = val.city; //供方所属地市
  editForm.yfPayee = val.payee; //供方收款人
  editForm.yfBankName = val.bankName; //供方开户银行
  editForm.yfBankAccount = val.bankAccount; //供方开户账号
  editForm.yfNationality = val.nationality; //供方国别
  editForm.yfIsOverseas = val.isOverseas; //供方是否海外单位
  editForm.note = val.note; //备注
  cityList.value = useCommonFetch("甲方所属地市", editForm.yfArea);
};

//上传合同附件改变
const htFileChange = () => {
  const ids = htFileList.value.map((item) => {
    const id = item.response ? item.response.data : item.id;
    return id;
  });
  editForm.fileIds = ids.join(",");
};
//上传盖章附件改变
const gzFileChange = () => {
  const ids = gzFileList.value.map((item) => {
    const id = item.response ? item.response.data : item.id;
    return id;
  });
  editForm.signature = ids.join(",");
};
const emit = defineEmits(["save-success"]);
//点击保存 //1:暂存,2:提交,3:保存并通过"
const save = async (type) => {
  submitType.value = type;
  try {
    await editFormRef.value?.validate(async (valid) => {
      if (valid) {
        let res;
        let params = {
          ...editForm,
          submitType: type,
        };
        loading.value = true;
        if (operateId.value) {
          params.id = operateId.value;
          res = await editRecords(params);
        } else {
          res = await addRecords(params);
        }
        if (res.code === 200) {
          ElMessage({
            message: "操作成功！",
            type: "success",
          });
          dialogVisible.value = false;
          emit("save-success");
        }
      }
    });
  } catch (error) {
    console.log(error);
  } finally {
    loading.value = false;
  }
};
const handleOpen = () => {
  provinceList.value = useCommonFetch("甲方地区");
};
//关闭弹窗
const handleClose = () => {
  editFormRef.value.resetFields();
  editForm.sourceProjectId = "";
  editForm.sourceProjectType = "";
  editForm.sourceProjectTypeValue = "";
  editForm.sourceProjectClass = "";
  editForm.sourceSubjectId = "";
  editForm.sourceSubjectClassId = "";
  editForm.chargerCode = "";
  editForm.taskId = "";
  editForm.isAuditor = "";
  //清空附件列表
  htFileList.value = []; //合同附件列表
  gzFileList.value = []; //盖章附件列表
  checkStatus.value = "";
  loading.value = false;
};
</script>

<style scoped lang="scss">
@import url("@/views/project/index.scss");
</style>
